//
//  LTQOrginal.swift
//  WeiBoProject
//
//  Created by MAC on 16/3/2.
//  Copyright © 2016年 LTQ. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

let CellMargin = 10

class LTQOrginal: UIView {
    
    var bottomConstraint: Constraint?
    
    var statusViewModel:LTQStatusViewModel?{
        didSet{
            nameLabel.text = statusViewModel?.statusModel?.user?.screen_name
            contentLabel.text = statusViewModel?.statusModel?.text
            veriftyImageView.image = statusViewModel?.verifity_image
            levelImageView.image = statusViewModel?.level_image
            let url = NSURL(string: (statusViewModel?.statusModel?.user?.profile_image_url)!)
            photoImageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            /*
            显示不显示配图视图
            当有配图的时候,展示配图,修改约束
            没有的时候,就隐藏,修改约束
            */
            // 配图视图数组有配图,并且count > 0
            
            //清空约束
            bottomConstraint?.uninstall()
            
           if let pic_urls = statusViewModel?.statusModel?.pic_urls where pic_urls.count > 0{
            
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                  bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
            
//            pictureView.pictureViewModel = statusViewModel
            var nmArray = [LTQPictureViewModel]()
            for model in pic_urls {
                
                let viewModel = LTQPictureViewModel()
                
                viewModel.model = model
                
                nmArray.append(viewModel)
            }
            pictureView.pictureList = nmArray
            
           }else{
            pictureView.hidden = true
            self.snp_updateConstraints(closure: { (make) -> Void in
                bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
            })
            }
            
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        addSubview(photoImageView)
        addSubview(nameLabel)
        addSubview(veriftyImageView)
        addSubview(levelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
//添加图片
        addSubview(pictureView)
        
        //头像约束 -- 左-10 上边 10 宽 40 高 40
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(self.snp_top).offset(CellMargin)
            make.size.equalTo(CGSizeMake(40, 40))
        }
        photoImageView.layer.cornerRadius = 20
        photoImageView.layer.masksToBounds = true
        //认证图片的约束 -- 中心点
        veriftyImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        // 名字 --左边 上边
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.top.equalTo(photoImageView.snp_top)
        }
        //等级 -- 左边 中心线
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(CellMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        // 时间的约束 -- 左 10,上边 5
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.top.equalTo(nameLabel.snp_bottom).offset(CellMargin/2)
        }
        // 来源 -- 左边 10 ,中心线
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(CellMargin)
            make.centerY.equalTo(timeLabel.snp_centerY)
        }
        // 设置内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(photoImageView.snp_bottom).offset(CellMargin)
            make.width.equalTo(UIScreen.mainScreen().bounds.width - 2 * CGFloat(CellMargin))
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        //告诉视图的底部是哪里
        self.snp_makeConstraints { (make) -> Void in
            bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(CellMargin).constraint
        }
        
    }
    
    
    
    //MARK:--懒加载
    ///         头像
    private lazy var photoImageView:UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "avatar_default_big")
        return img
        }()
    
    ///      认证图片
    private lazy var veriftyImageView:UIImageView = {
        
        let img = UIImageView()
        img.image = UIImage(named: "avatar_vip")
        return img
        }()
    
    ///      微博名字
    private lazy var nameLabel:UILabel = {
        let lab = UILabel()
        lab.text = "微博名字"
        lab.font = UIFont.systemFontOfSize(15)
        return lab
        }()
    
    ///      会员等级
    private lazy var levelImageView:UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "common_icon_membership_level1")
        return img
        }()
///     发表时间
    private lazy var timeLabel:UILabel = {
        let lab = UILabel()
        lab.text = "时间"
        lab.font = UIFont.systemFontOfSize(13)
        lab.textColor = UIColor.orangeColor()
        return lab
        }()
    
    
    ///      发表来源
    private lazy var sourceLabel:UILabel = {
        let lab = UILabel()
        lab.text = "微博来源"
        lab.font = UIFont.systemFontOfSize(13)
        return lab
        }()
    
    private lazy var contentLabel:UILabel = {
        let lab = UILabel()
        lab.text = "微博内容"
        lab.font = UIFont.systemFontOfSize(15)
        lab.numberOfLines = 0
        return lab
        }()

    
    
    private lazy var pictureView: LTQPictureView = LTQPictureView()

}
